from django.shortcuts import render

# Create your views here.
# from django.contrib.auth import get_user_model
from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view
from rest_framework.response import Response
from activity_sys.models import Activity
from account_sys.models import User
from follow_sys.models import *
from enterprise_sys.models import *
from utils.response_utils import build_response
from utils.session_utils import has_login, get_login_user

# User = get_user_model()

@csrf_exempt
@api_view(['GET'])
#获取关注的用户动态
def get_recommendations_from_user(request):
    if not has_login(request):
        return build_response(errno=2001, msg='用户未登录')

    user = get_login_user(request)

    # 获取用户关注的用户
    followed_users = Follow_user.objects.filter(follower=user).values_list('followee_user', flat=True)

    # 获取关注用户的最新动态
    user_activities = Activity.objects.filter(user__in=followed_users).order_by('time')

    # 将动态数据转换为字典形式
    # user_activity_list = {activity.id: activity.parse_dict() for activity in user_activities}
    user_activity_list = list(activity.id for activity in user_activities)

    # 构建返回数据
    recommendations = {
        'userActivities': user_activity_list,
    }

    return build_response(other_info=recommendations, msg='获取推荐动态成功')

def get_recommendations_from_enterprise(request):
    if not has_login(request):
        return build_response(errno=2001, msg='用户未登录')

    user = get_login_user(request)

    # 获取用户关注的企业
    followed_enterprises = Follow_enterprise.objects.filter(follower=user).values_list('followee_enterprise', flat=True)

    # 获取关注企业的最新动态
    # enterprise_activities = .objects.filter(enterprise__in=followed_enterprises).order_by('time')

    # enterprise_activity_ids = list(activity.id for activity in enterprise_activities)

    # 获取关注企业中的所有用户
    enterprise_user_ids = Enterprise_Usr.objects.filter(enterprise__in=followed_enterprises).values_list('usr_id',flat=True)

    # 获取这些用户的最新动态
    enterprise_activities = Activity.objects.filter(user__in=enterprise_user_ids).order_by('-time')

    enterprise_activity_list = list(activity.id for activity in enterprise_activities)

    recommendations = {
        'enterpriseActivities': enterprise_activity_list,
    }

    return build_response(other_info=recommendations, msg='获取推荐动态成功')


@csrf_exempt
@api_view(['GET'])
def get_job_recommendations(request):
    if not has_login(request):
        return build_response(errno=2001, msg='用户未登录')

    user = get_login_user(request)

    rejobs = Job.objects.all().order_by('?')[:5]
    job_ids = [rejob.id for rejob in rejobs]
    # 获取用户的意向岗位
    intended_positions = user.intended_position

    if  intended_positions:
        #return build_response(errno=4001, msg='用户未设置意向岗位')

    # 将意向岗位字符串拆分为列表
        intended_positions_list = intended_positions.split(',')

    # 根据每个意向岗位搜索相关职位
        recommended_jobs = Job.objects.none()  # 初始化为空的QuerySet
        for position in intended_positions_list:
            recommended_jobs = recommended_jobs | Job.objects.filter(name__icontains=position.strip())

    # 限制返回的职位数量
    # recommended_jobs = recommended_jobs.order_by('enterprise_id', '-id')[:10]

        recommended_jobs = recommended_jobs.order_by('enterprise_id', '-id')
        job_ids1 = [job.id for job in recommended_jobs]
        job_ids.extend(job_ids1)
        job_ids.reverse()
    #print(rejobs)

    # 构建返回数据
    recommendations = {
        'jobIds': job_ids,
    }

    return build_response(other_info=recommendations, msg='获取推荐职位成功')

@csrf_exempt
@api_view(['GET'])
def get_enterprise_recommendations(request):
    if not has_login(request):
        return build_response(errno=2001, msg='用户未登录')

    user = get_login_user(request)
    reusers = Enterprise.objects.all().order_by('?')[:5]
    enterprise_ids = [reuser.id for reuser in reusers]
    #测试
    # print(user)
    # 获取用户的意向岗位
    intended_positions = user.intended_position

    if intended_positions:
        #return build_response(errno=4001, msg='用户未设置意向岗位')

    # 将意向岗位字符串拆分为列表
        intended_positions_list = intended_positions.split(',')

    # 根据每个意向岗位搜索相关职位，并获取相关企业ID
        recommended_enterprises = set()
        for position in intended_positions_list:
            jobs = Job.objects.filter(name__icontains=position.strip())
            for job in jobs:
                recommended_enterprises.add(job.enterprise.id)

    # 构建返回数据
        print(recommended_enterprises)
        enterprise_ids.extend(recommended_enterprises)
        enterprise_ids.reverse()

    return build_response(other_info={'enterpriseIds': enterprise_ids}, msg='获取推荐企业成功')

@csrf_exempt
@api_view(['GET'])
def get_user_recommendations(request):
    if not has_login(request):
        return build_response(errno=2001, msg='用户未登录')

    user = get_login_user(request)
    reusers = User.objects.all().order_by('?')[:5]
    user_ids = [reuser.id for reuser in reusers]
    # 获取用户的意向岗位
    intended_positions = user.intended_position

    if intended_positions:
        #return build_response(errno=4001, msg='用户未设置意向岗位')

    # 将意向岗位字符串拆分为列表
        intended_positions_list = intended_positions.split(',')

    # 根据每个意向岗位搜索相关用户，并获取相关用户ID
        recommended_users = set()
        for position in intended_positions_list:
            users = User.objects.filter(intended_position__icontains=position.strip()).exclude(id=user.id)
            for u in users:
                recommended_users.add(u.id)

    # 构建返回数据
        user_ids.extend(recommended_users)
        user_ids.reverse()
    return build_response(other_info={'userIds': user_ids}, msg='获取推荐用户成功')